package com.example.archive.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.archive.entity.Archive;
import com.example.archive.entity.ArchiveModificationRequest;
import com.example.archive.mapper.ArchiveModificationRequestMapper;
import com.example.archive.service.ArchiveModificationRequestService;
import com.example.archive.service.ArchiveService;
import com.example.archive.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class ArchiveModificationRequestServiceImpl extends ServiceImpl<ArchiveModificationRequestMapper, ArchiveModificationRequest> 
        implements ArchiveModificationRequestService {
    
    @Autowired
    private SystemLogService systemLogService;
    
    @Autowired
    private ArchiveService archiveService;
    
    @Override
    public Page<ArchiveModificationRequest> getRequestsWithDetails(int current, int size) {
        Page<ArchiveModificationRequest> page = new Page<>(current, size);
        return baseMapper.selectRequestsWithDetails(page);
    }
    
    @Override
    public boolean submitModificationRequest(ArchiveModificationRequest request) {
        request.setApplyTime(LocalDateTime.now());
        request.setStatus("pending");
        request.setCreatedAt(LocalDateTime.now());
        request.setUpdatedAt(LocalDateTime.now());
        
        boolean result = save(request);
        
        if (result) {
            systemLogService.recordLog(request.getApplicantId(), "提交档案修改申请", 
                "申请修改档案ID：" + request.getArchiveId());
        }
        
        return result;
    }
    
    @Override
    public boolean approveModificationRequest(Integer requestId, String status, String remark, Integer approverId) {
        ArchiveModificationRequest request = getById(requestId);
        if (request != null) {
            request.setStatus(status);
            request.setApproveRemark(remark);
            request.setApproverId(approverId);
            request.setApproveTime(LocalDateTime.now());
            request.setUpdatedAt(LocalDateTime.now());
            
            boolean result = updateById(request);
            
            if (result) {
                systemLogService.recordLog(approverId, "审批档案修改申请", 
                    "审批申请ID：" + requestId + "，结果：" + status);
            }
            
            return result;
        }
        return false;
    }
    
    @Override
    public boolean executeModification(Integer requestId, String executeRemark, Integer executorId) {
        ArchiveModificationRequest request = getById(requestId);
        if (request != null && "approved".equals(request.getStatus())) {
            try {
                // 1. 获取要修改的档案
                Archive archive = archiveService.getById(request.getArchiveId());
                if (archive == null) {
                    return false;
                }
                
                // 2. 使用申请中的修改内容直接覆盖档案的OCR内容
                archive.setOcrContent(request.getModifyContent());
                archive.setLastModifiedBy(executorId);
                archive.setUpdatedAt(new Date());
                
                // 调用档案服务更新档案
                boolean archiveUpdateResult = archiveService.updateById(archive);
                if (!archiveUpdateResult) {
                    return false;
                }
                
                // 3. 更新申请状态
                request.setStatus("completed");
                request.setExecuteRemark(executeRemark);
                request.setExecutorId(executorId);
                request.setExecuteTime(LocalDateTime.now());
                request.setUpdatedAt(LocalDateTime.now());
                
                boolean result = updateById(request);
                
                // 4. 记录系统日志
                if (result) {
                    systemLogService.recordLog(executorId, "执行档案内容修改", 
                        "执行修改申请ID：" + requestId + "，档案ID：" + request.getArchiveId() + "，已更新OCR内容");
                }
                
                return result;
            } catch (Exception e) {
                // 记录错误日志
                systemLogService.recordLog(executorId, "执行档案修改失败", 
                    "执行修改申请ID：" + requestId + "，错误：" + e.getMessage());
                return false;
            }
        }
        return false;
    }
} 